package enron;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

public class SpinnerRenderer extends JLabel implements TableCellRenderer {
	private static final long serialVersionUID = 2188050115055249301L;
	protected TableCellRenderer delegate;
	protected JSpinner spinner;

	public SpinnerRenderer(SpinnerNumberModel cellSpinnerModel, TableCellRenderer defaultRenderer) {
		delegate = defaultRenderer;
		//identifies TableCellRenderer
		setName("Table.spinnerCellRenderer");
		setLayout(new BorderLayout());

		spinner = new JSpinner(cellSpinnerModel);   
		//remove component border
		spinner.setBorder(new EmptyBorder(0,0,0,0));
		//disable component background painting
		spinner.setOpaque(false);
		((JSpinner.NumberEditor)spinner.getEditor()).getTextField().setBackground(new Color(0,0,0,0));
		add(spinner);
	}

	public Component getTableCellRendererComponent(JTable table, Object value, 
			boolean isSelected, boolean hasFocus, int row, int column) {
		JComponent c = (JComponent)delegate.getTableCellRendererComponent(table, value, 
				isSelected,hasFocus, row, column);
		//respect insets and focus
		setBorder(c.getBorder());
		//copy opacity
		setOpaque(c.isOpaque());
		//apply background
		setBackground(c.getBackground());    
		//apply foreground
		spinner.getEditor().setForeground(c.getForeground());
		//set value
		if(value == null) {
			spinner.setValue(0L);
		} else {
//			System.out.println("value is "+value+", type is "+value.getClass().getName());
			spinner.setValue(value);
		}
		return this;
	}
}
